package com.hns.can.safety.web;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.common.domain.GridPage;
import com.hns.can.common.service.CommonInitParamService;
import com.hns.can.common.web.BaseController;
import com.hns.can.safety.entity.AppMftPcsDetailEntity;
import com.hns.can.safety.service.ReportFaultHandleService;
import com.hns.can.safety.vo.ReportFaultHandleParamsVo;
import com.hns.can.safety.vo.ReportFaultHandleVo;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.pub.DateTool;

/**
 * 报警处理controller
 * @author:zhangy
 * @email:zhangyu2222z@sina.cn
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("reportFaultHandle")
public class ReportFaultHandleController extends BaseController {

	@Autowired
	private ReportFaultHandleService reportFaultHandleService;
	
	@Autowired
	private CommonInitParamService commonInitParamService;
	
	final private String[] headerNames=new String[]{"所属机构", "营运线路", "车牌号", "VIN", "故障单号", "生成时间", "更新时间", "故障单状态", "故障名称", "故障发生时间","故障修复时间"};
    final private String[] headerFields = new String[]{"organName", "lineName", "licPltNo", "VIN", "faultHandleNo", "createTime", "updateTime", "faultHandleFormSt", "mftTypeName","mftOccurTime","mftRepTime"};
	
	/**
	 * 查询报警处理
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@RequestMapping("searchReportFaultHandleDetail")
	@ResponseBody
	public Object searchReportFaultHandleDetail(ReportFaultHandleParamsVo params) throws Exception{
		Pagination page = reportFaultHandleService.searchReportFaultHandleDetail(params);
		GridPage gridPage = new GridPage(page);
		return this.getReturnResult(gridPage);
	}
	
	
	@RequestMapping("exportReportFaultHandleExcel")
	public void exportReportFaultHandleExcel(ReportFaultHandleParamsVo params, HttpServletRequest request) throws Exception{
		List list = getExportFaultHandleData(params);
		ExportUtil.exportFiles(Arrays.asList(headerNames), Arrays.asList(headerFields), list, response, "excel", 
				URLEncoder.encode("报警处理明细", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "报警处理明细");
	}
	
	
	@RequestMapping("exportReportFaultHandlePdf")
	public void exportReportFaultHandlePdf(ReportFaultHandleParamsVo params, HttpServletRequest request) throws Exception{
		List list = getExportFaultHandleData(params);
		ExportUtil.exportFiles(Arrays.asList(headerNames), Arrays.asList(headerFields), list, response, "pdf", 
				URLEncoder.encode("报警处理明细", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "报警处理明细");
	}
	
	@RequestMapping("uptFaultHandleInfo")
	@ResponseBody
	public Object uptFaultHandleInfo(AppMftPcsDetailEntity appMftPcsDetailEntity){
		reportFaultHandleService.uptFaultHandleInfo(appMftPcsDetailEntity);
		return this.getReturnResult();
	}
	
	
	@RequestMapping("getFaultHandleInfoById")
	@ResponseBody
	public Object getFaultHandleInfoById(ReportFaultHandleParamsVo params){
		AppMftPcsDetailEntity entity = reportFaultHandleService.getFaultHandleInfoById(params);
		return this.getReturnResult(entity);
	}
	
	
	private List getExportFaultHandleData(ReportFaultHandleParamsVo params) throws Exception{
		List list = reportFaultHandleService.getExportFaultHandleData(params);
		return list;
	}
	
	
	@RequestMapping("getFaultHandleFormInfo")
	@ResponseBody
	public Object getFaultHandleFormInfo(ReportFaultHandleParamsVo params) throws Exception{
		ReportFaultHandleVo entity = reportFaultHandleService.getFaultHandleFormInfo(params);
		return this.getReturnResult(entity);
	}
	
	
	@RequestMapping("getFaultHandleHistoryInfo")
	@ResponseBody
	public Object getFaultHandleHistoryInfo(ReportFaultHandleParamsVo params){
		commonInitParamService.dateType2DateTime(params, params.getDateType());
		ReportFaultHandleVo entity = reportFaultHandleService.getFaultHandleHistoryInfo(params);
		return this.getReturnResult(entity);
	}
	
}
